import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate';
Vue.use(Vuex)

const PERSIST_PATHS = ['account', 'loginstate', 'local', 'login', 'loginindex', 'goods'];
export default new Vuex.Store({
    plugins: [createPersistedState({
        paths: PERSIST_PATHS
    })],
    state: {
        login: [],
        account: "",
        goods: [],
        loginstate: false,
        loginindex: '',
        local: '',
    },
    getters: {
        goodsPrice(state) {
            let length = state.goods.length
            return length
        },
        goodsNum(state) {
            let length = state.goods.length
            return length
        },
        totalAmount(state) {
            let selGoods = state.goods.filter(i => i.isSelete)
            let amount = 0
            for (let i = 0; i < selGoods.length; i++) {
                amount += selGoods[i].number * selGoods[i].price
            }
            return amount
        },
        payCart(state) {
            let selGoods = state.goods.filter(i => i.isSelete)
            return selGoods
        }
    },

    mutations: {
        changelogin(state, a) {
            state.login.push(a)
        },
        changeaccount(state, b) {
            state.account = b
        },
        changeLoginstate(state, c) {
            state.loginstate = c
        },
        changelocal(state, d) {
            state.local = d
        },
        changeindex(state, e) {
            state.loginindex = e
        },
        addGoods(state, goodsContent) {
            // if(goodsContent.content)
            let isadd = false
            let adds = ''
            for (let i = 0; i < state.goods.length; i++) {
                if (state.goods[i].content != goodsContent.content) {
                    isadd = false
                } else {
                    isadd = true
                    adds = i
                    break
                }
            }
            if (isadd == false) {
                state.goods.push(goodsContent)
            } else {
                state.goods[adds].number += goodsContent.number
            }
        },
        addNum(state, i) {
            state.goods[i].number++
        },
        subNum(state, i) {
            if (state.goods[i].number > 1) {
                state.goods[i].number--
            } else {
                return
            }
        },

        delGoods(state, id) {
            state.goods = state.goods.filter(i => i.id != id)
        },

        toggleSelete(state, i) {
            state.goods[i].isSelete = !state.goods[i].isSelete
        },

        addOrder(state) {
            state.goods = state.goods.filter(i => !i.isSelete)
        }
    },
    actions: {},
    modules: {}
})